import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        user: sessionStorage.getItem('user'),
        isLogin: sessionStorage.getItem('isLogin'),
        img: sessionStorage.getItem('list')?JSON.parse(sessionStorage.getItem('list')):[{
            img: require("../assets/images/oppo.jpg"),
            name: 'OPPO R15 只能手机 全网通',
            num: 2,
            unitPrice: 599,
            isSelect: true
        },{
            img : require("../assets/images/vivo.jpg"),
            name : "vivo X27 8GB+256GB大内存",
            num : 1,
            unitPrice : 699,
            isSelect : true
        }]
    },
    getters: {
        length: function(state){
            return state.list.length
        },
        list: function(state){
            var payList = state.list.filters(function(item) {
                if(item.isSelect == true){
                    return item
                }
            })
            return payList;
        }
    },
    mutations: {
        login: function(state,user){
            state.user = user;
            state.isLogin = true;
            sessionStorage.setItem('user',user);
            sessionStorage.setItem('isLogin',true);
        },
        logout: function(state){
            state.user = null;
            state.isLogin = false;
            sessionStorage.removeItem('user');
            sessionStorage.removeItem('isLogin');
        },
        getList: function(state,obj){
            var arr = [];
            state.list.forEach(function (item){
                arr.push(item.name);
            });
            if(arr.indexOf(obj.goodsInfo.name) === -1){
                obj.goodsInfo.num += obj.number;
                state.list.unshift(obj.goodsInfo);
            }else{
                state.list[arr.indexOf(obj.goodsInfo.name)].num += obj.number;
            }
            sessionStorage.setItem('list',JSON.stringify(state.list));
        },
        removeGoods: function (state,index) {
            state.list.splice(index,1);
            sessionStorage.setItem('list',JSON.stringify(state.list));
        },
        selectGoods: function (state,obj) {
            state.list[obj.index].isSelect = obj.bool;
            sessionStorage.setItem('list',JSON.stringify(state.list));
        },
        selectAll: function (state,bool) {
            for(var i = 0;i < state.list.length; i++){
              state.list[i].isSelect = bool;
            }
            sessionStorage.setItem('list',JSON.stringify(state.list));
        },
        reduce: function (state,index) {
            var goods = state.list[index];
            if(goods.num >= 2){
              goods.num--;
            }
            sessionStorage.setItem('list',JSON.stringify(state.list));
        },
        add: function (state,index) {
            var goods = state.list[index];
            goods.num++;
            sessionStorage.setItem('list',JSON.stringify(state.list));
        },
        emptyCar: function (state) {
            state.list.splice(0,state.list.length);
            sessionStorage.setItem('list',JSON.stringify(state.list));
        }
    },
    actions: {
        loginAction: function(context, user){
            context.commit('login', user);//提交mutation
        },
        logoutAction: function(context){
            context.commit('logout');//提交mutation
        },
      getListAction: function(context, obj){
        context.commit('getList', obj);
      },
      removeGoodsAction: function(context, index){
        context.commit('removeGoods', index);
      },
      selectGoodsAction: function(context, obj){
        context.commit('selectGoods', obj);
      },
      selectAllAction: function(context, bool){
        context.commit('selectAll', bool);
      },
      reduceAction: function(context, index){
        context.commit('reduce', index);
      },
      addAction: function(context, index){
        context.commit('add', index);
      },
      emptyCarAction: function(context){
        context.commit('emptyCar');
      }
    }
})